﻿using System;
using System.Collections.Generic;
using System.Web;
using Softserve.Pottery.Infrastructure;

namespace Pottery.Frontend.Helpers
{
    public static class CookieHelper
    {
        public const int DEFAULT_SESSION_TIMEOUT = 20000;

        public static void CreateCookie(string cookieName, string value, int? expirationDays)
        {
            ValidateHttpContext();

            HttpCookie cookie = new HttpCookie(cookieName, value);

            if (expirationDays.HasValue)
                cookie.Expires = DateTime.Now.AddDays(expirationDays.Value);

            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static void DeleteCookie(string cookieName)
        {
            ValidateHttpContext();

            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];

            if (cookie != null)
                HttpContext.Current.Response.Cookies.Remove(cookieName);
        }

        public static bool CookieExists(string cookieName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];

            if (cookie != null)
                return true;

            return false;
        }

        public static Dictionary<string, string> GetAllCookies()
        {
            ValidateHttpContext();

            Dictionary<string, string> cookies = new Dictionary<string, string>();

            foreach (string key in HttpContext.Current.Request.Cookies.AllKeys)
                cookies.Add(key, HttpContext.Current.Request.Cookies[key].Value);

            return cookies;
        }

        public static void DeleteAllCookies()
        {
            ValidateHttpContext();

            HttpCookieCollection cookies = HttpContext.Current.Request.Cookies;

            foreach (HttpCookie cookie in cookies)
                DeleteCookie(cookie.Name);
        }

        public static string GetCookieValue(string cookieName)
        {
            ValidateHttpContext();

            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookieName);

            if (cookie != null)
                return cookie.Value;

            return null;
        }

        public static void SetCookieValue(string cookieName, string value, uint? expirationSeconds)
        {
            ValidateHttpContext();

            HttpCookieCollection cookieContainer = HttpContext.Current.Response.Cookies;
            HttpCookie httpCookie = new HttpCookie(cookieName, value);
            
            if (expirationSeconds.HasValue)
                httpCookie.Expires = DateTime.Now.AddSeconds(expirationSeconds.Value);

            cookieContainer.Set(httpCookie);
        }

        private static void ValidateHttpContext()
        {
            if (HttpContext.Current == null)
                throw new ArgumentNullException(Constants.HTTP_CONTEXT_IS_NULL);

            if (HttpContext.Current.Request == null)
                throw new ArgumentNullException(Constants.REQUEST_IS_NULL);

            if (HttpContext.Current.Request.Cookies == null)
                throw new ArgumentNullException(Constants.COOKIES_IS_NULL);
        }
    }
}